<template>
  <div style="margin: 0 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px">
      <el-form-item label="进件账号" prop="userName">
        <el-input
          style="width: 180px"
          v-model="queryParams.userName"
          placeholder="请输入子账号"
          clearable
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="进件姓名" prop="name">
        <el-input
          style="width: 180px"
          v-model="queryParams.name"
          placeholder="请输入进件姓名"
          clearable
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="身份证" prop="cardNo">
        <el-input
          style="width: 180px"
          v-model="queryParams.cardNo"
          placeholder="请输入身份证"
          clearable
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="手机号" prop="tel">
        <el-input
          style="width: 180px"
          v-model="queryParams.tel"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="queryList">
      <el-table-column label="进件ID" align="center" prop="queryId"/>
      <el-table-column label="子账号ID" align="center" prop="userId"/>
      <el-table-column label="子账号" align="center" prop="userName"/>
      <el-table-column label="进件姓名" align="center" prop="name"/>
      <el-table-column label="身份证" align="center" prop="cardNo" width="170"/>
      <el-table-column label="手机号" align="center" prop="tel"/>
      <el-table-column label="评分" align="center" prop="score"/>
      <el-table-column label="决策" align="center" prop="decision"/>
      <el-table-column label="三要素认证" align="center" prop="threeElmentAuth" width="120"/>
      <el-table-column label="进件时间" align="center" prop="createTime" width="150"/>
      <el-table-column label="进件备注" align="center" prop="moreInfo"/>
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="openReport(scope.row)">查看风险
          </el-button>

          <el-button
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:query:edit']">修改
          </el-button>
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:query:remove']">删除
          </el-button>
          <el-button
            type="text"
            @click="addBlackList(scope.row)">加入行业黑名单
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>
    <!-- 添加或修改进件记录对话框 -->
    <el-dialog title="修改进件记录" :visible.sync="open" :close-on-click-modal="false" class="my_dialog" width="450px">
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="身份证" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="请输入身份证"/>
        </el-form-item>
        <el-form-item label="手机号" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="进件备注" prop="moreInfo">
          <el-input v-model="form.moreInfo" placeholder="请输入进件备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="邀请查询" :visible.sync="addReferOpen" :destroy-on-close="true" width="550px" class="my_dialog">
      <inviteQuery v-if="addReferOpen" :item="item"/>
    </el-dialog>

    <el-dialog title="加入行业黑名单" :visible.sync="openAddBlackList" width="500px" :close-on-click-modal="false">
      <el-form ref="blackListForm" :model="blackListForm" :rules="blackListRules" label-width="80px">
        <el-form-item label="真实姓名" prop="name">
          <el-input v-model="blackListForm.name" placeholder="请输入姓名" :disabled="true"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="cardNo">
          <el-input v-model="blackListForm.cardNo" placeholder="请输入身份证" :disabled="true"/>
        </el-form-item>
        <el-form-item label="手机号码" prop="tel">
          <el-input v-model="blackListForm.tel" placeholder="请输入手机号" :disabled="true"/>
        </el-form-item>
        <el-form-item label="风险标签" prop="labels">
          <el-select style="width: 100%;" v-model="blackListForm.labels" multiple
                     filterable allow-create placeholder="请选择或输入风险标签">
            <el-option
              v-for="item in labelsOptions"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发生地址">
          <el-input v-model.trim="blackListForm.location" placeholder="请输入地址"/>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model.trim="blackListForm.remark" type="textarea" placeholder="请输入内容。具体行为，过程，金额等"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitBlackList">确 定</el-button>
        <el-button @click="openAddBlackList=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {getQuery, delQuery, updateQuery, subListByParent} from "@/api/system/query";
  import {validateContacts, validateIdNo, validatePhone} from "@/utils/validate"
  import inviteQuery from "@/views/components/inviteQuery.vue"
  import {labels, addBlacklist} from "@/api/system/blacklist";

  export default {
    name: "subQuery",
    components: {inviteQuery},
    data() {
      let NCT = (rule, value, callback) => {
        if (!(this.blackListForm.name ||
          this.blackListForm.cardNo ||
          this.blackListForm.tel)) {
          callback(new Error("姓名/身份证号/手机号至少填一项"))
        } else {
          //任意值被填写，清除验证提示
          if (this.blackListForm.name) {
            validateContacts(rule, this.blackListForm.name, callback)
          }
          if (this.blackListForm.cardNo) {
            validateIdNo(rule, this.blackListForm.cardNo, callback)
          }
          if (this.blackListForm.tel) {
            validatePhone(rule, this.blackListForm.tel, callback)
          }
          callback()
        }
      }
      return {
        labelsOptions: [],
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 进件记录表格数据
        queryList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        openAys: false,

        addReferOpen: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userName: undefined,
          costResult: undefined,
          name: undefined,
          cardNo: undefined,
          tel: undefined
        },
        item: undefined,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            {min: 2, max: 20, message: '姓名长度必须介于 2 和 20 之间', trigger: 'blur'},
            {validator: validateContacts, trigger: 'blur'}
          ],
          cardNo: [
            {validator: validateIdNo, trigger: 'blur'}
          ],
          tel: [
            {validator: validatePhone, trigger: 'blur'}
          ]
        },
        openAddBlackList: false,
        blackListForm: {},
        blackListRules: {
          name: [
            {min: 2, max: 20, message: '姓名长度必须介于 2 和 20 之间', trigger: 'blur'},
            {required: false, validator: NCT, trigger: "blur"}
          ],
          cardNo: [
            {required: false, validator: NCT, trigger: "blur"}
          ],
          tel: [
            {required: false, validator: NCT, trigger: "blur"}
          ],
          labels: [
            {required: true, message: '请选择或输入风险标签', trigger: "blur"}
          ]
        },
      }
    },
    created() {
      this.getList();
    },
    methods: {
      openQuery(item) {
        updateQuery({
          queryId: item.queryId,
          sourceType: 2
        })
        this.item = item
        this.addReferOpen = true
      },
      openReport(item) {
        this.$router.push({
          path: "/query/report",
          query: {id: item.queryId, sort: 1}
        })
      },
      addBlackList(item) {
        this.blackListForm = {
          name: item.name,
          cardNo: item.cardNo,
          tel: item.tel,
          labels: null,
          location: null,
          remark: null
        };
        this.resetForm("blackListForm");
        labels().then(res => {
          this.labelsOptions = res.data
          this.openAddBlackList = true
        })
      },
      submitBlackList() {
        this.$refs.blackListForm.validate(valid => {
          console.log(valid)
          if (valid) {
            addBlacklist({
              ...this.blackListForm,
              labels: JSON.stringify(this.blackListForm.labels)
            }).then(response => {
              this.$modal.msgSuccess("成功");
              this.openAddBlackList = false;
            });
          }
        });
      },
      /** 查询进件记录列表 */
      getList() {
        this.loading = true;
        subListByParent(this.queryParams).then(response => {
          this.queryList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          queryId: undefined,
          userId: undefined,
          name: undefined,
          cardNo: undefined,
          tel: undefined,
          moreInfo: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const queryId = row.queryId || this.ids
        getQuery(queryId).then(response => {
          this.form = response.data;
          this.open = true;
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateQuery(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const queryIds = row.queryId || this.ids;
        this.$modal.confirm('是否确认删除进件记录编号为"' + queryIds + '"的数据项？').then(function () {
          return delQuery(queryIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      }
    }
  };
</script>
<style scoped>
  ::v-deep .my_dialog .el-dialog__body {
    /*margin: 20px;*/
    padding: 12px 22px;
  }
</style>
